//
//  SinaFMDBTool.swift
//  MySinaWeiBoSwiftProject
//
//  Created by apple on 2016/11/26.
//  Copyright © 2016年 WangChen. All rights reserved.
//

import UIKit
import FMDB


class SinaSqliteTool: NSObject {
    
    static let share: SinaSqliteTool = SinaSqliteTool()
    
    let queue: FMDatabaseQueue
    
    override init() {
        
        let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("weibo.db")
        
        //打开DB文件(若不存在就创建)
        queue = FMDatabaseQueue(path: path)
        
        super.init()
        //创建表
        creatTable()
        
    }
    
    
    //建表
    private func creatTable(){
        let sql = " CREATE TABLE IF NOT EXISTS t_statuses (statueId integer NOT NULL,statue text,userId text,createDate text DEFAULT (datetime('now','localtime')),PRIMARY KEY(statueId));"
        
        queue.inTransaction { (db, roolBack) in
            let res = db!.executeStatements(sql)
            if res{
                print("建表成功")
            }else{
                print("建表失败")
                roolBack?.pointee = true
            }
        }
    }
    
    
    
}



/*
 CREATE TABLE IF NOT EXISTS "t_statuses" (
"statueId" integer NOT NULL,
"statue" text,
"userId" text,
"createDate" text DEFAULT (datetime('now','localtime')),
PRIMARY KEY("statueId")
);
 */



